package com.changgou.goods.controller;


import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.service.SkuService;
import com.changgou.goods.pojo.Sku;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/sku")
public class SkuController {


    @Autowired
    private SkuService skuService;

    /**
     * @param spuId
     * @return com.changgou.entity.Result
     * @description: 根据spu的id查询sku的数据
     * @author 大佬味的小男孩
     * @date 2020/07/22 18:37
     */
    @GetMapping("spu/{spuId}")
    public List<Sku> findSkuListBySpuid(@PathVariable("spuId") String spuId) {
        HashMap<String, Object> searchMap = new HashMap<>();
        //如果接收的的参数不是all,那么查询指定id
        if (!"all".equals(spuId)) {
            searchMap.put("spuId", spuId);
        }
        //如果接收的的参数是all, 那么查询审核状态为1的商品
        searchMap.put("status", "1");
        List<Sku> skuList = skuService.findList(searchMap);
        System.out.println("根据spu的id查询sku的数据和:" + skuList.size());
        return skuList;
    }

    /**
     * @param id
     * @return com.changgou.entity.Result<com.changgou.goods.pojo.Sku>
     * @description: 根据id查询sku的商品数据
     * @author 大佬味的小男孩
     * @date 2020/08/01 11:01
     */
    @GetMapping("/{id}")
    public Result<Sku> findById(@PathVariable("id") String id) {
        Sku sku = skuService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", sku);
    }

    /**
     * @param username
     * @return com.changgou.entity.Result
     * @description: 1.获取购物车的数据 2.循环扣减库存并增加销量
     * @author 大佬味的小男孩
     * @date 2020/08/03 17:16
     */
    @PostMapping("/decr/count")
    public Result decrCount(@RequestParam("username") String username) {
        skuService.decrCount(username);
        return new Result(true, StatusCode.OK, "库存扣减成功");
    }

    /**
     * @param orderId
     * @param num
     * @return com.changgou.entity.Result
     * @description: 回滚库存：增加库存并扣减销量
     * @author 大佬味的小男孩
     * @date 2020/08/06 10:26
     */
    @RequestMapping("resumeStockNum")
    public Result resumeStockNum(@RequestParam("skuId") String skuId, @RequestParam("num") Integer num) {
        skuService.Increasing(skuId, num);
        return new Result(true, StatusCode.OK, "回滚订单成功");
    }

}